﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Microsoft.Devices.Sensors;
using Spinning.Spinning.Unitity;
using Spinning.Spinning.Entity;
using Spinning.Spinning;
using Spinning.Spinning.Level;
using Spinning.Spinning.Entity.Scenes;
using Microsoft.Phone.Info;
using Spinning.Spinning.Scenes.SinglePlayer;
using Spinning.Spinning.Entity.Scenes.SinglePlayer;
using Spinning.Spinning.Scenes.BattleScene;


namespace Spinning
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        static public Accelerometer _acceleMeter;
        public static StageManager stageManager;
        public static GraphicsDevice graphicsDevice;
        public static String hostId = "";
        public static String clientId = "";
        public static TouchCollection TouchCollection { get; private set; }

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.SupportedOrientations = DisplayOrientation.Portrait;
            graphics.PreferredBackBufferHeight = Constants.Window_Height;
            graphics.PreferredBackBufferWidth = Constants.Window_Width;
            graphics.IsFullScreen = true;
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            // Extend battery life under lock.
            InactiveSleepTime = TimeSpan.FromSeconds(1);
            _acceleMeter = new Accelerometer();
            TouchPanel.EnabledGestures = GestureType.Pinch | GestureType.FreeDrag;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Game1.graphicsDevice = graphics.GraphicsDevice;

            

            ResourceManager.Initialize(Content);

            stageManager = new StageManager();
            stageManager.LoadState();

            BaseScene scene = new StartScene();
            stageManager.AddScene("start", scene);

            scene = new PracticeWaterScene();
            stageManager.AddScene("water", scene);

            scene = new PracticeWindScene();
            stageManager.AddScene("wind", scene);

            scene = new PracticeFireScene();
            stageManager.AddScene("fire", scene);

            scene = new MenuScene();
            stageManager.AddScene("menu", scene);

            scene = new TeachWaterScene();
            stageManager.AddScene("teachWater", scene);

            scene = new TeachFireScene();
            stageManager.AddScene("teachFire", scene);

            scene = new TeachWindScene();
            stageManager.AddScene("teachWind", scene);

            scene = new SuccessScene("water");
            stageManager.AddScene("successWater", scene);

            scene = new SuccessScene("fire");
            stageManager.AddScene("successFire", scene);

            scene = new SuccessScene("wind");
            stageManager.AddScene("successWind", scene);

            scene = new TestScene();
            stageManager.AddScene("test", scene);

            scene = new UdpTestScene();
            stageManager.AddScene("udp", scene);

            scene = new QRreaderScene();
            stageManager.AddScene("qr", scene);

            scene = new ConnectScene();
            stageManager.AddScene("connect", scene);

            scene = new UserEarnBattleScene("host");
            stageManager.AddScene("lifeHost", scene);
            scene = new HostScene();
            stageManager.AddScene("host", scene);

            scene = new UserEarnBattleScene("client");
            stageManager.AddScene("lifeClient", scene);
            scene = new ClientScene();
            stageManager.AddScene("client", scene);

            scene = new SinglePlayerScene();
            stageManager.AddScene("single", scene);

            /////////stage1
            scene = new UserEarnLifeTimeScene("stage1");
            stageManager.AddScene("lifeStage1", scene);
            scene = new SinglePlayerStage1Scene();
            stageManager.AddScene("stage1", scene);

            ///////stage1Boss
            scene = new UserEarnLifeTimeScene("stage1Boss");
            stageManager.AddScene("lifeStage1Boss", scene);
            scene = new SinglePlayerStage1BossScene();
            stageManager.AddScene("stage1Boss", scene);

            /////////stage2
            scene = new UserEarnLifeTimeScene("stage2");
            stageManager.AddScene("lifeStage2", scene);
            scene = new SinglePlayerStage2Scene();
            stageManager.AddScene("stage2", scene);

            ///////stage2Boss
            scene = new UserEarnLifeTimeScene("stage2Boss");
            stageManager.AddScene("lifeStage2Boss", scene);
            scene = new SinglePlayerStage2BossScene();
            stageManager.AddScene("stage2Boss", scene);

            scene = new RoleScene();
            stageManager.AddScene("role", scene);

            scene = new StoreScene();
            stageManager.AddScene("store", scene);

            scene = new ItemScene();
            stageManager.AddScene("item", scene);

            scene = new SinglePlayerMenuScene();
            stageManager.AddScene("singleMenu", scene);

            scene = new CalRewardScene();
            stageManager.AddScene("reward", scene);

            scene = new SingleGameAnimation();
            stageManager.AddScene("animation", scene);

            scene = new SinglePlayerAnimationBoss1();
            stageManager.AddScene("animationBoss1", scene);

            scene = new SinglePlayerAnimationBoss2();
            stageManager.AddScene("animationBoss2", scene);

            scene = new SinglePlayerAnimationBoss1End();
            stageManager.AddScene("animationBoss1End", scene);

            scene = new SinglePlayerAnimationBoss2End();
            stageManager.AddScene("animationBoss2End", scene);

            scene = new SingleGameAnimationEnd();
            stageManager.AddScene("animationEnd", scene);

            MusicManager.AddMusic("water1_wind2", "water1_wind2", 0.001f, false);
            MusicManager.SetRePlay(true, "water1_wind2");

            MusicManager.AddMusic("boss", "boss", 0.001f, false);
            MusicManager.SetRePlay(true, "boss");

            MusicManager.AddMusic("menu", "menu", 0.001f, false);
            MusicManager.SetRePlay(true, "menu");

            stageManager.GoToScene("start");

            
            //MusicManager.PlayMusic("menu");

            hostId = GetDeviceUniqueID();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            //if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            //    this.Exit();
            TouchCollection = TouchPanel.GetState();
            // TODO: Add your update logic here
            base.Update(gameTime);
            stageManager.OnUpdate(gameTime);
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                stageManager.SaveState();
                this.Exit();
            }
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphicsDevice.Clear(Color.CornflowerBlue);
            base.Draw(gameTime);
            stageManager.OnDraw(spriteBatch, gameTime);

        }

        public static String GetDeviceUniqueID()
        {
            object DeviceUniqueID;

            byte[] DeviceIDbyte = null;

            if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out DeviceUniqueID))

                DeviceIDbyte = (byte[])DeviceUniqueID;

            string DeviceID = Convert.ToBase64String(DeviceIDbyte);

            return DeviceID;
        }
    }
}
